using System;
using System.Diagnostics;
using System.Globalization;
using System.Resources;
using Pharmacy.Ultilities.Localization;

namespace Pharmacy.Ultilities.Resources
{
	/// <summary>
	/// Base class for reading assembly resources.
	/// </summary>
	/// <author></author>
	/// <history>
	/// 2011.04.06 - ThangTran: removed unused function GetInt(), GetByte(), Get...() to avoid conflict with new feature: Localzation (returned object always has type string).
	/// </history>
	public class ResourcesBase
	{
		protected ResourceManager Resources;

		/// <summary>
		/// Initializes the resources.
		/// </summary>
		/// <param name="resourceName">File Name without extension of the resource.</param>
		public ResourcesBase(string resourceName)
		{
			Type myType = GetType();
			Resources = new ResourceManager(string.Format("{0}.{1}", myType.Namespace, resourceName), myType.Module.Assembly);
		}

		#region Methods

		public object GetObject(string name)
		{
			return GetObject(null, name);
		}

        /// <summary>
        /// Gets object in the resource.
        /// </summary>
        /// <param name="culture">Reference to the culture info.</param>
        /// <param name="name">Name of the resource.</param>
        /// <returns></returns>
        /// <author></author>
        /// <history>
        /// 2011.04.06 - ThangTran: added xml-doc, added code that tries to use localizated text first.
        /// </history>
		public object GetObject(CultureInfo culture, string name)
		{
            return LocalizerText.CanGetText(name) ? LocalizerText.GetText(name, null) : Resources.GetObject(name, culture);
		}

		public string GetString(string name)
		{
			return GetString(null, name);
		}

        /// <summary>
        /// Gets string in the resource.
        /// </summary>
        /// <param name="culture">Reference to the culture info.</param>
        /// <param name="name">Name of the resource.</param>
        /// <returns></returns>
        /// <author></author>
        /// <history>
        /// </history>
		public string GetString(CultureInfo culture, string name)
		{
            string defaultText =string.Empty;
            if (LocalizerText.CanGetText(name) == false)
                defaultText = Resources.GetString(name, culture);
            return LocalizerText.GetText(name, defaultText); 
		}

		public string GetString(string name, params object[] args)
		{
			return GetString(null, name, args);
		}

		public string GetString(CultureInfo culture, string name, params object[] args)
		{
			string format = Resources.GetString(name, culture);
		    Debug.Assert(format != null);
			return string.Format(format, args);
		}
		#endregion Methods
	}
}
